package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.QueryObject.BannerQuery;
import cn.wolfcode.luowowo.domain.Banner;
import cn.wolfcode.luowowo.respository.DestinationRespository;
import cn.wolfcode.luowowo.respository.BannerRespository;
import cn.wolfcode.luowowo.service.IBannerService;
import cn.wolfcode.luowowo.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Optional;

@Service
//@Transactional  暂时不管事务
public class BannerServiceImpl implements IBannerService {

    @Autowired
    private BannerRespository bannerrRespository;

    @Autowired
    private IBannerService bannerService;

    @Autowired
    private DestinationRespository destinationRespository;


    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public Banner get(String id) {
        Optional<Banner> optional = bannerrRespository.findById(id);
        if (optional.isPresent()) {
            return optional.get();
        }
        return null;
    }

    @Override
    public List<Banner> list() {
        return bannerrRespository.findAll();
    }

    @Override

    public Page<Banner> query(BannerQuery qo) {
        //使用工具类
        Query query = new Query();
        PageRequest of = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.DESC, "sequence");
        qo.setPageable(of);
        return DBHelper.query(mongoTemplate, Banner.class, query, qo.getPageable());
    }

    @Override
    public void saveOrUpdate(Banner banner) {
        if (!StringUtils.hasLength(banner.getId())) {
            banner.setId(null);
            bannerrRespository.save(banner);
        } else {
            Query query = new Query();
            query.addCriteria(Criteria.where("_id").is(banner.getId()));
            DBHelper.update(mongoTemplate, query, banner,
                    "refId", "title", "subTitle", "coverUrl",
                    "state", "sequence", "type");
        }
    }

    @Override
    public void changeHotValue(String id, int hot) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        Update update = new Update();
        update.set("ishot", hot);
        mongoTemplate.updateMulti(query, update, Banner.class);
    }

    @Override
    public void delete(String id) {
        bannerrRespository.deleteById(id);
    }

    @Override
    public List<Banner> queryBanner(int type, int state) {
        Query query = new Query();
        PageRequest of = PageRequest.of(0, 5, Sort.Direction.DESC, "sequence");
        return bannerrRespository.findByTypeAndState(type, state, of);
    }
}
